<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <title>分享云微商城</title>

    <!-- css -->
    <link href="../../../Public/Resource/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../../Public/Resource/css/all.css" rel="stylesheet">
    <script type="text/javascript" src="//libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="../../../Public/Resource/js/common.js"></script>
    <script language='javascript' src='../../../Public/Resource/js/jquery.cookie.js'></script>
    <script language='javascript' src='../../../Public/Resource/js/wechat-useragent.js'></script>
</head>
<body>


<div class="navbar-default" role="navigation">
    <div class="container-fluid">
        <ul class="nav">
            <li><a href="Index/index">微商场</a></li>
            <li><a href="#">购物车</a></li>
            <li><a href="Index/about">分享云简介</a></li>
        </ul>
    </div>
</div>
<div class="main">
    <div class="container-fluid">
        <div class="row" id="locklist">
            <!--<div class="col-xs-12 product-list car-list">-->
                <!--<div class="car-img">-->
                    <!--<input type="radio" />-->
                    <!--<img src="img/lock.jpg" alt="">-->
                <!--</div>-->
                <!--<div class="car-name">-->
                    <!--<p>-->
                        <!--<a >互联网智能门锁 X600 ( 防盗门 ) <i class="glyphicon glyphicon-trash" style="float: right;color: #a8a8a8"></i></a>-->
                    <!--</p>-->
                    <!--<p style="color: #a8a8a8">规格：左开 内开</p>-->
                    <!--<div>-->
                        <!--<span style="color:#e84e40">￥399</span>-->
                        <!--<div class="num" style="float: right">-->
                            <!--<input class="min" name="" type="button" value="-" onclick="subnum(event);" />-->
                            <!--<input class="text_box" name="goodnum" type="text" value="1" onchange="setTotal(event.target)"/>-->
                            <!--<input class="add" name="" type="button" value="+" onclick="addnum(event);" />-->
                        <!--</div>-->
                    <!--</div>-->
                <!--</div>-->
            <!--</div>-->

        </div>
    </div>
    <div class="footer">
        <ul class="nav nav-thr">
            <li class="my-cart">
                <span><input type="checkbox" name="checkAll" id="checkAll" onclick="checkAll()"> 全选</span>
                <span id="totalprice">合计 : ￥0</span>
            </li>
            <li class="btn-red"><a href="#" onclick="topay()" id="totalnum">去结算</a></li>
        </ul>
    </div>
</div>



<script>



    //重要不能删
    $.removeCookie('buynow',{ path: '/' });

    var locklist = document.getElementById('locklist');
    var goodkeys = $.cookie('goodkeys');

//    alert("goodkeys = "+goodkeys);
    if(!isOwnEmpty(goodkeys)){

        var keys = goodkeys.split('@');

        for(var i=0;i<keys.length;i++) {
            var key = keys[i];

            var data = key.split('-');
            var lockid = data[0];
            var lr = data[1];
            var io = data[2];

            var imageurl = "/Resource/img/lock.jpg";
            var lockname = "";
            var lockprice = "";
            var specification = lr+" "+io;
            specification = specification.replace('left','左开').replace('right','右开')
                         .replace('in','内开').replace('out','外开');


            if(lockid == 600){
                imageurl = "/Resource/img/600z.png";
                lockname = "<?php echo ($x600name); ?>";
                lockprice = "<?php echo ($x600saleprice); ?>";
            }else if(lockid == 610){
                imageurl = "/Resource/img/610z.png";
                lockname = "<?php echo ($x610name); ?>";
                lockprice = "<?php echo ($x610saleprice); ?>";
            }else if(lockid == 666){
                imageurl = "/Resource/img/gateway.png";
                lockname = "<?php echo ($x666name); ?>";
                lockprice = "<?php echo ($x666saleprice); ?>";
            }else if(lockid == 777){
                imageurl = "/Resource/img/box.png";
                lockname = "<?php echo ($x777name); ?>";
                lockprice = "<?php echo ($x777saleprice); ?>";
            }


            var parentdiv = document.createElement("div");
            parentdiv.setAttribute('id', key);
            parentdiv.setAttribute('class', 'col-xs-12 product-list car-list');

            var carimg = document.createElement("div");
            carimg.setAttribute('class', "car-img");
            parentdiv.appendChild(carimg);

            var radioinput = document.createElement("input");
            radioinput.setAttribute('type', "checkbox");
            radioinput.setAttribute('name', "subBox");
            radioinput.setAttribute('id', "select_"+key);
            radioinput.setAttribute('onclick',"onitemselect(event)");
            carimg.appendChild(radioinput);

            var image = document.createElement("img");
            image.setAttribute('src', imageurl);
            carimg.appendChild(image);


            var carname = document.createElement("div");
            carname.setAttribute('class', "car-name");
            parentdiv.appendChild(carname);

            var p = document.createElement("p");
            //href="+"'/Lockdetail/index/lockid/"+lockid +"'
            p.innerHTML = "<a  >"+lockname+
                    "<i class='glyphicon glyphicon-trash' " +
                    "style='float: right;color: #a8a8a8'  onclick='deleteitem(event)'>" +
                    "<input type='hidden' id='getdeletekey' class='getdeletekey' value='"+key+"'"+"></i></a>";
            carname.appendChild(p);

            var p1 = document.createElement("p");
            p1.setAttribute('style', "color:#a8a8a8");

            p1.innerHTML = "规格："+specification;

            if(lockid == '666' || lockid == '777'){
                p1.style.visibility = 'hidden';
            }else{
                p1.style.visibility = 'visible';
            }
            carname.appendChild(p1);


            var divprice = document.createElement("div");
            carname.appendChild(divprice);

            var price = document.createElement("span");
            price.setAttribute('style', 'color:#e84e40');
            price.innerHTML = "￥"+lockprice;
            divprice.appendChild(price);

            var divnum = document.createElement("div");
            divnum.setAttribute('style', "float: right");
            divnum.setAttribute('class', 'num');
            divprice.appendChild(divnum);

            var inputsub = document.createElement("input");
            inputsub.setAttribute('type', "button");
            inputsub.setAttribute('class', 'sub');
            inputsub.setAttribute('value', '-');
            inputsub.setAttribute('id',"sub_"+key);
            inputsub.setAttribute('onclick', 'subnum(event);');
            divnum.appendChild(inputsub);

            var inputgoodnum = document.createElement("input");
            inputgoodnum.setAttribute('type', "text");
            inputgoodnum.setAttribute('name', "goodnum");
            inputgoodnum.setAttribute('class', 'text_box');
            inputgoodnum.setAttribute('value', $.cookie(key));
            inputgoodnum.setAttribute('id',"goodnum_"+key);
            inputgoodnum.setAttribute('onchange', 'setTotal(event.target)');
            divnum.appendChild(inputgoodnum);

            var inputadd = document.createElement("input");
            inputadd.setAttribute('type', "button");
            inputadd.setAttribute('class', 'add');
            inputadd.setAttribute('value', '+');
            inputadd.setAttribute('id',"add_"+key);
            inputadd.setAttribute('onclick', 'addnum(event);');
            divnum.appendChild(inputadd);


            locklist.appendChild(parentdiv);


        }
        document.getElementById('checkAll').checked = true;
        checkAll();
    }

    function topay() {
        var subs = $("input[name=subBox]");
        if(subs.filter(":checked").length == 0){
            alert("请选择商品支付");
        }else{
            window.location = "/order/index";
        }
    }

    function onitemselect(e) {
//        alert(e.target.id+"\n"+$.cookie(e.target.id));
         var radioselect = document.getElementById(e.target.id);
        if(radioselect.checked){
            $.cookie(e.target.id,'1',{ expires: 7, path: '/' });
        }else{
            $.cookie(e.target.id,'0',{ expires: 7, path: '/' });
        }

//        alert(e.target.id+"\n"+$.cookie(e.target.id));
        calculateprice();

        var checkAll = $("input[name=checkAll]");
        var subs = $("input[name=subBox]");
//        alert(subs.length+"  == "+subs.filter(":checked").length);
        checkAll.prop("checked" , subs.length == subs.filter(":checked").length ? true :false);
    }

    function checkAll() {
        var checkAll = document.getElementById('checkAll');
        if(checkAll.checked == true){
            $("input[name=subBox]").prop("checked", true);
            setCheckStatus("1");
            calculateprice();
        }else{
            $("input[name=subBox]").prop("checked", false);
            setCheckStatus("0");
            var span_totalprice = document.getElementById('totalprice');
            var a_totalprice = document.getElementById('totalnum');
            a_totalprice.innerHTML = "去结算";
            span_totalprice.innerHTML = "合计 : ￥0";
        }




    }
    function calculateprice() {
            var keys = $.cookie('goodkeys').split('@');
            var totalprice = 0;
            var totalnum = 0;
            for(var i=0;i<keys.length;i++) {
                var key = keys[i];
                var ischeck = $.cookie("select_"+key);
                if(!isOwnEmpty(ischeck) && typeof(ischeck) != undefined && ischeck == '1'){
    //                alert("calculateprice key:"+key);
                    totalprice =totalprice + parseInt($.cookie(key))*getlockprice(key.split('-')[0]);
                    totalnum = totalnum + parseInt($.cookie(key));
                }
            }

            var span_totalprice = document.getElementById('totalprice');
            var a_totalprice = document.getElementById('totalnum');
            a_totalprice.innerHTML = "去结算("+totalnum+")";
            span_totalprice.innerHTML = "合计 : ￥"+totalprice;

    //        alert("总价:"+totalprice);

    }

    function getlockprice(lockid) {
        var lockprice = "0";//单价
        if(lockid == 600){
            lockprice = "<?php echo ($x600saleprice); ?>";
        }else if(lockid == 610){
            lockprice = "<?php echo ($x610saleprice); ?>";
        }else if(lockid == 666){
            lockprice = "<?php echo ($x666saleprice); ?>";
        }else if(lockid == 777){
            lockprice = "<?php echo ($x777saleprice); ?>";
        }

        return lockprice;
    }

    function setCheckStatus(boolean) {
        var keys = $.cookie('goodkeys').split('@');
        for(var i=0;i<keys.length;i++) {
            var key = keys[i];
            $.cookie("select_"+key,boolean,{ expires: 7, path: '/' });

        }
    }



    function deleteitem(e) {
        var p = e.target.parentNode;
        var t = p.getElementsByClassName('getdeletekey')[0];
        var locklist = document.getElementById('locklist');
        var item = document.getElementById(t.value);
        $.removeCookie(t.value,{ path: '/' });

        var goodkeys = $.cookie('goodkeys');

        goodkeys = goodkeys.replace(t.value+"@","").replace('@'+t.value,"").replace(t.value,"");
        $.cookie('goodkeys',goodkeys,{ expires: 7, path: '/' });

//        alert(temp+"\n"+goodkeys);

        locklist.removeChild(item);
//       alert(t.value+"  \n "+item.id);

        calculateprice();
    }

    function subnum(e){
        var p = e.target.parentNode;
        var t = p.getElementsByClassName('text_box')[0];
        t.value = parseInt(t.value)-1;
        setTotal(t,e.target.id);

    }

    function addnum(e){
        var p = e.target.parentNode;
        var t = p.getElementsByClassName('text_box')[0];
        t.value = parseInt(t.value)+1;
        setTotal(t,e.target.id);
    }

    function setTotal(t,name){
        if(isOwnEmpty(name) || typeof name == undefined || name == ""){
            name = t.id;
        }
        var tt = t.value;
        if(parseInt(tt)<=0||isNaN(tt)){
            t.value = 1;
        }
         var temp = parseInt(t.value);
         var key = name.split('_')[1];


//        alert(key+"  原数量:"+$.cookie(key)+" 数量:"+temp);

        $.cookie(key,temp,{ expires: 7, path: '/' });

        calculateprice();

    }


</script>



</body>
</html>